import axios from 'axios';
import {UserState} from '@/store/modules/user/types';
import {Md5} from 'ts-md5';
import {PageParams, PageRes} from "@/api/common";

const baseUrl = '/sc/user/';
const apis = {
  login: `${baseUrl}login`,
  logout: `${baseUrl}logout`,
  getUserInfo: `${baseUrl}getUserInfo`,
  register: `${baseUrl}register`,
  queryPage:`${baseUrl}page`,
  delete:`${baseUrl}delete`,
  addForm:`${baseUrl}add`,
  editForm:`${baseUrl}edit`,
  detail:`${baseUrl}detail`,
  unOrLock:`${baseUrl}unOrLock`,
  roleIds:`${baseUrl}roleIds`,
  roleAssign:`${baseUrl}roleAssign`
};

export interface LoginData {
  account: string;
  password: string;
}

export interface RegisterData {
  account: string;
  password: string;
  name: string;
  email: string;
}

export interface UserModel {
  id?: undefined,
  avatar?: string,
  account?: string,
  name?: string,
  nickname?: string,
  gender: undefined,
  age?: string,
  birthday?: string,
  nation?: string,
  phone?: string,
  email?: string,
  perms?:string[],
}


export function login(data: LoginData) {
  const param = {} as LoginData;
  Object.assign(param, data);
  param.password = Md5.hashStr(data.password);
  return axios.post<string>(apis.login, param);
}

export function logout() {
  return axios.post<string>(apis.logout);
}

export function getUserInfo() {
  return axios.get<UserState>(apis.getUserInfo);
}

export function register(data: RegisterData) {
  return axios.post<string>(apis.register, data);
}

export function queryPage(params: PageParams) {
  return axios.post<PageRes>(apis.queryPage, params);
}

export function removeRow(rowId: string) {
  return axios.delete<string>(`${apis.delete}?id=${rowId}`);
}

export function add(data: any) {
  const param = {} as any;
  Object.assign(param, data);
  param.password = Md5.hashStr(data.password);
  param.passwordAgain =  Md5.hashStr(data.passwordAgain);
  return axios.post<string>(apis.addForm, param);
}

export function edit(data: any) {
  return axios.post<string>(apis.editForm, data);
}

export function detail(rowId:string) {
  return axios.get<any>(`${apis.detail}?id=${rowId}`);
}

export function unOrLock(params: any) {
  return axios.post<string>(apis.unOrLock, params);
}

export function roleIds(rowId: string) {
  return axios.get<string[]>(`${apis.roleIds}?id=${rowId}`);
}


export function roleAssign(data: any) {
  return axios.post<string>(apis.roleAssign, data);
}
